package PhoneLog.PhoneLog2;



import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class PhoneReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
    //keyIn-->Text （电话号码） (reducer的输入类型是map的输出端类型)
    //valueIn-->FlowBean（FlowBean封装好的属性）
    //keyOut-->Text  （电话号码）
    //valueOUt-->FlowBean的统计和


    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
       //(13560436666,{FlowBean(1213,1213)},{FlowBean(121,213)})
       //1.获取数据遍历FlowBean
        long sum_upFlow =0;//定义上行流量的总和
        long sum_downFlow =0;//定义下行流量的总和
        long sum=0;
        for ( FlowBean flowBean: values) {
              sum_upFlow+=flowBean.getUpFlow();
              sum_downFlow+=flowBean.getDownFlow();

        }
        sum=sum_upFlow+sum_downFlow;
        //2.封装数据
        FlowBean sumFlowBean= new FlowBean(sum_upFlow,sum_downFlow,sum);
        //3.写出数据
        context.write(key,sumFlowBean);
    }
}

